/*
 * Main logic code
 *
 * This is part of the game The Rooms
 * Copyright (C) 2010  Elvio Sarquis
 *
 * Get the GNU GPL v3 in http://www.gnu.org/licenses/
 */

package rooms;

import utils.UtilsArrays;
import utils.XML;
import utils.Point;
import rooms.items.*;

/**
 *
 * @author dask
 */
public class Engine {

    private Character[] chars;
    private Map map;
    private Item[] items;
    private int inmenu;
    //Variables del buffer de mensajes
    private String[] buffer;

    public Engine() {

        inmenu = 0;
        chars = new Character[1];
        chars[0] = new PC();
        map = new Map();
        buffer = new String[1];
        buffer[0] = "";
        items = new Item[0];

        NPC a = new NPC(7, 8);
        addChar(a);

        a = new NPC(1, 5);
        addChar(a);

        a = new NPC(5, 11);
        addChar(a);

        a = new NPC(5, 1);
        addChar(a);

        Item b = new Armor("Casco", "Casco para cabesear pupos", 2, 2, 15, 20, C.BROWN, ')', C.HEAD, 1, 3, 5);
        addItem(b);

        b = new Container("Mochila", "Eso", 6, 6, 10, 20, C.BLUE, '/', 5, 200);
        addItem(b);

        map.refactor(chars);
    }

    public void load() {
        chars = null;
        map = null;

        XML.load("save");

        map = XML.getMap();
        chars = XML.getChars();

        map.refactor(chars);
    }

    public void endTurn() {

        for (int i = 1; i < chars.length; i++) {

            if (!chars[i].getAlive()) {
                addToBuffer(chars[i].getName() + " died.");
                removeChar(chars[i].getId());
            }
        }

        map.refactor(chars);

        if (!chars[0].getAlive()) {
            addToBuffer("You died.");
            inmenu = C.imDEAD;
        }
    }

    public void addChar(Character newone) {

        newone.setId(chars.length);

        Character[] tempo = new Character[chars.length + 1];

        for (int i = 0; i < chars.length; i++) {

            tempo[i] = chars[i];
        }

        tempo[chars.length] = newone;

        chars = tempo;
    }

    public void removeChar(int id) {

        Character[] tempo = new Character[chars.length - 1];

        for (int i = 0; i < chars.length; i++) {

            if (chars[i].getId() < id) {
                tempo[i] = chars[i];
            } else if (chars[i].getId() > id) {
                chars[i].setId(i - 1);
                tempo[i - 1] = chars[i];
            }
        }

        chars = new Character[tempo.length];

        for (int i = 0; i < tempo.length; i++) {

            chars[i] = tempo[i];
        }
    }

    public Character getChar(int id) {
        return chars[id];
    }

    public Character[] getChars() {
        return chars;
    }

    public void addItem(Item newone) {

        Item[] temp = new Item[items.length + 1];

        for (int i = 0; i < items.length; i++) {
            temp[i] = items[i];
        }

        items = new Item[temp.length];
        items = temp;
        items[items.length - 1] = newone;
    }

    public void removeItem(int index) {

        Item[] tempo = new Item[items.length - 1];

        for (int i = 0; i < items.length; i++) {

            if (i < index) {
                tempo[i] = items[i];
            } else if (i > index) {
                tempo[i - 1] = items[i];
            }
        }

        items = new Item[tempo.length];
        for (int i = 0; i < tempo.length; i++) {

            items[i] = tempo[i];
        }
    }

    public Item getItem(int index) {
        return items[index];
    }

    public Item[] getItems() {
        return items;
    }

    public Item[] getPCItems() {

        Item[] pcitems = new Item[15];

        return pcitems;
    }

    public boolean command(int command) {
        PC ch = (PC) chars[0];
        int mapto = 0, deltax = 0, deltay = 0;
        boolean b = false;

        if (Keys.isMoveKey(command)) {

            if (command == Keys.UPLEFT) {
                deltax = -1;
                deltay = -1;
            } else if (command == Keys.UP || command == Keys.ARROWUP) {
                deltax = 0;
                deltay = -1;
            } else if (command == Keys.UPRIGHT) {
                deltax = 1;
                deltay = -1;
            } else if (command == Keys.LEFT || command == Keys.ARROWLEFT) {
                deltax = -1;
                deltay = 0;
            } else if (command == Keys.RIGHT || command == Keys.ARROWRIGHT) {
                deltax = 1;
                deltay = 0;
            } else if (command == Keys.DOWNLEFT) {
                deltax = -1;
                deltay = 1;
            } else if (command == Keys.DOWN || command == Keys.ARROWDOWN) {
                deltax = 0;
                deltay = 1;
            } else if (command == Keys.DOWNRIGHT) {
                deltax = 1;
                deltay = 1;
            }

            mapto = map.getBumpMapPoint(ch.getX() + deltax, ch.getY() + deltay);

            if (mapto <= 0) {

                move(0, deltax, deltay);

            } else if (mapto < 10) {

                switch (mapto) {

                    case C.WALL:
                        addToBuffer("ouch");
                        break;

                    case C.CDOOR:
                        map.change(ch.getX() + deltax, ch.getY() + deltay, C.ODOOR);
                        addToBuffer("You open the door");
                        break;
                }

            } else if (mapto > 10) {

                attack(0, mapto - 10);
            }
            return true;

        } else if (Keys.isActionKey(command)) {
            if (command == Keys.ESC) {
                XML.save(map, chars, "save.xml");
                System.exit(0);
            } else if (command == Keys.INVENT) {
                inmenu = C.imINVENT;
                return true;
            } else if (command == Keys.PICKUP) {
                for (int i = 0; i < items.length; i++) {
                    System.out.println(items.length + ":" + i);
                    if (items[i].getX() == ch.getX() && items[i].getY() == ch.getY()) {
                        if (!ch.addIvent(items[i])) break;
                        addToBuffer("You pick up the " + items[i].getName());
                        removeItem(i);
                        b = true;
                    }
                }
                if(!b) {
                    addToBuffer("Thers nothig to pick");
                    return false;
                }
            } else if (command == Keys.WAIT) {
                addToBuffer("You Wait");
                return true;
            }
        } else if (Keys.isEtcKey(command)) {
        }
        return true;
    }

    public void addToBuffer(String line) {

        String[] tempo = new String[buffer.length + 1];

        for (int i = 0; i < buffer.length; i++) {

            tempo[i] = buffer[i];
        }

        tempo[buffer.length] = line;

        buffer = new String[tempo.length];

        for (int i = 0; i < tempo.length; i++) {

            buffer[i] = tempo[i];
        }
    }

    public String[] getBuffer() {

        String[] tempo = new String[1];
        tempo[0] = "";
        int line = 0;

        for (int i = 0; i < buffer.length; i++) {

            if ((tempo[line].length() + buffer[i].length() + 1) <= 50) {
                tempo[line] = tempo[line] + " " + buffer[i];
            } else {
                tempo = UtilsArrays.IncreseStringArray(tempo);
                line++;
                tempo[line] = buffer[i];
            }
        }

        buffer = new String[1];
        buffer[0] = "";

        return tempo;
    }

    public Map getMapa() {
        return map;
    }

    public int getInMenu() {
        return inmenu;
    }

    private void move(int who, int deltax, int deltay) {
        Character ch = getChar(who);

        ch.setX(ch.getX() + deltax);
        ch.setY(ch.getY() + deltay);
    }

    private void attack(int idfrom, int idto) {

        chars[idto].changeHP(-1);

        addToBuffer(chars[idfrom].getName() + " attacks " + chars[idto].getName()
                + " for 1 points of damage");
    }

    public void turnAI() {

        AI.setAI(chars, map);
        Point to;

        for (int i = 1; i <= chars.length - 1; i++) {

            switch (AI.getAction(i)) {

                case 0:                        //wait
                    break;
                case 1:                        //move
                    to = AI.getTo();
                    move(i, to.x() - chars[i].getX(), to.y() - chars[i].getY());
                    break;
                case 2:                        //Attacks
                    attack(i, 0);
                    break;
            }
        }
        AI.cls();
    }
}
